package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.ReportService;
import com.itheima.health.vo.ReportVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.itheima.health.service.MemberService;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.SimpleDateFormat;
import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {

    @Resource
    private ReportService reportService;

    /**
     * 获取过去一定时间内的业务报表数据
     * @return 业务报表数据
     */
//    @GetMapping("/getBusinessReportData")
//    public Result getBusinessReportData() {
//        log.info("获取过去一定时间内的业务报表数据");
//            return new Result(true,
//                    MessageConst.GET_BUSINESS_REPORT_SUCCESS,
//                    this.reportService.getBusinessReportData());
//    }
    @Autowired
    private MemberService memberService;
    @Autowired
    private SetMealService setMealService;



    @GetMapping("/getMemberReport")
    public Result getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy-MM").format(date));
        }
        map.put("months", months);
        List<Integer> memberCount = memberService.findMemberCountByMonths(months);
        map.put("memberCount", memberCount);
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, map);
    }

    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> setmealCount = setMealService.findSetmealCount();
        data.put("setmealCount", setmealCount);
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : setmealCount) {
            String name = (String) map.get("name");
            setmealNames.add(name);

        }
        data.put("setmealNames", setmealNames);
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, data);
    }

    /**
     * 运营数据统计
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        ReportVo reportVo = reportService.getBusinessReportData();
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS,reportVo);
    }

    /**
     * 数据统计报表导出
     */
    @GetMapping("/exportBusinessReport")
    public Result exportExcel(HttpServletResponse response) {
        reportService.exportExcel(response);
        return new Result(true,MessageConst.GET_BUSINESS_REPORT_SUCCESS);
    }

}
